﻿
//编辑对话框
var dlg = new MyAppDialog(700, 500);

//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload)
        $("#datagrid").datagrid("reload");
}
var cols = [[
    { field: "Id", title: 'Id', width: 10, hidden: true, hight: 25 },
    { field: "ck", title: '', checkbox: true, width: 30, align: 'center' },
    { field: "DocKey", title: "编号", sortable: true, width: 100, align: 'left' },//编号
    { field: "Title", title: "名称", sortable: true, width: 100, align: 'left' },//名称
    {
        field: "Updatetime", title: "更新时间", sortable: true, width: 100, align: 'left'
    },//修改时间
    {
        field: "Updateuser", title: "更新人", sortable: true, width: 50, align: 'left'
    },//修改人
]];
$(document).ready(function () {
    //判断是否有编辑权限
    if (canEdit) {
        $("#btndel").show();
        $("#btnadd").show();
    }
    //删除
    $("#btndel").click(function (evt) { evt.preventDefault(); del(); });
    //添加
    $("#btnadd").click(function (evt) {

        evt.preventDefault();
        dlg.setSize(1000, 600);
        dlg.setURL(csms_lang.btn_add, baseurl + "Basic/Document/Edit/0?c=" + $("#Code").val() + "&" + Math.random());
        dlg.show();
    });

    //查询
    $("#btnquery").click(function (evt) {
        evt.preventDefault();
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        $("#datagrid").datagrid("load");
    });

    $("#searchkey").keydown(function (event) {
        if (event.keyCode == $.ui.keyCode.ENTER) {
            event.preventDefault();
            $("#btnquery").click();
            return false;
        }
    });
    LoadDatagrid();
});


//加载列表数据
function LoadDatagrid() {
    var $dg = $("#datagrid");
    $dg.datagrid({
        //title: csms_lang.puble_lable_company_list,
        url: baseurl + 'Basic/Document/DocumentJson',
        fit: true,
        fitColumns: true,
        striped: true,
        nowrap: true,
        idField: 'Id',
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 45, 60, 75],
        rownumbers: true,
        singleSelect: false,
        queryParams: {
            s: $("#searchkey").val()
        },
        sortName: 'Id',
        sortOrder: 'asc',
        columns: cols,
        onLoadSuccess: function () {
            //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
            $dg.datagrid("clearSelections");
        },
        onDblClickRow: function (rowIndex, row) {
            dlg.setSize(1000, 600);
            dlg.setURL(csms_lang.public_lable_edit, baseurl + "Basic/Document/Edit/" + row.Id + "?" + Math.random());
            dlg.show();
        },
        //onSelect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //},
        //onUnselect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //},
        toolbar: "#datagrid_toolbar"
    });
}

//删除
function del() {
    var row = $("#datagrid").datagrid('getSelections');
    if (row.length == 0) {
        $.messager.alert(csms_lang.message_info_title, csms_lang.message_delete_choosedelete);
        return;
    }
    $.messager.confirm(csms_lang.message_confirm, csms_lang.message_delete_confrim, function (r) {
        if (r) {
            var ids = "";
            for (var i = 0; i < row.length; i++) {
                ids += row[i].Id + ",";
            }

            $.ajax({
                type: "post",
                url: baseurl + "Basic/Document/Delete",
                data: { ids: ids },
                success: function (e) {
                    if (e.Ret == "0") {
                        $("#datagrid").datagrid("reload");
                    } else {
                        $.messager.alert(csms_lang.message_info_title, e.Msg);
                    }
                }
            });
        }

    })
}